package com.yumis.service.modules.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yumis.service.common.enums.HttpState;
import com.yumis.service.common.exception.GlobalException;
import com.yumis.service.common.vo.Page;
import com.yumis.service.common.vo.Response;
import com.yumis.service.model.pojo.User;
import com.yumis.service.modules.user.mapper.UserMapper;
import com.yumis.service.modules.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        user.setCreateTime((int) (System.currentTimeMillis()/1000));
        user.setUpdateTime((int) (System.currentTimeMillis()/1000));
        int i=userMapper.insert(user);
        if (i!=1){
            throw new GlobalException(Response.buildResponse(HttpState.CREATED_FAILED));
        }
    }

    @Override
    public void updateUser(User user) {
        user.setUpdateTime((int) (System.currentTimeMillis()/1000));
        int i=userMapper.updateByPrimaryKeySelective(user);
        if(i != 1){
            throw new GlobalException(Response.buildResponse(HttpState.UPDATE_ERROR));
        }
    }

    @Override
    public void deleteUserById(Long id) {
        int i=userMapper.deleteByPrimaryKey(id);
        if(i != 1){
            throw new GlobalException(Response.buildResponse(HttpState.DELETE_ERROR));
        }
    }

    @Override
    public void deleteUser(Long[] ids) {
        for (Long id:ids){
            int i=userMapper.deleteByPrimaryKey(id);
            if(i != 1){
                throw new GlobalException(Response.buildResponse(HttpState.DELETE_ERROR));
            }
        }
    }

    @Override
    public Page<User> queryUser(Integer page, Integer rows) {
        //分页
        PageHelper.startPage(page,rows);
        //过滤
        Example example = new Example(User.class);
        example.setOrderByClause("create_time desc");
        List<User> userList=userMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userList)){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        PageInfo<User> info = new PageInfo<>(userList);
        return new Page<>(info.getTotal(),info.getPages(),userList);
    }

    @Override
    public List<User> searchUser(String key) {
        Example example = new Example(User.class);
        example.setOrderByClause("create_time desc");
        //如果输入框有值就进行模糊搜索，没有值查询所有的用户信息
        if(StringUtils.isNoneBlank(key)){//过滤条件
            example.createCriteria().orLike("name","%" + key + "%")
                                    .orLike("phoneOne","%"+key+"%")
                                    .orLike("phoneTwe","%"+key+"%");
        }
        List<User> userList=userMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userList)){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        return userList;
    }

    @Override
    public Page<User> searchUserByShop(Integer page, Integer rows,Long shopId) {
        //分页
        PageHelper.startPage(page, rows);
        //过滤
        Example example = new Example(User.class);
        example.setOrderByClause("create_time desc");
        List<User> userList = userMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(userList)) {
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        PageInfo<User> info = new PageInfo<>(userList);
        return new Page<>(info.getTotal(), info.getPages(), userList);
    }

    @Override
    public User searchUserById(Long userId) {
        User user=new User();
        user.setId(userId);
        User user1=userMapper.selectOne(user);
        if (user1 ==null || user1.equals("")){
            throw new GlobalException(Response.buildResponse(HttpState.DATA_NOT_FOUNT));
        }
        return user1;
    }
}
